package com.luocj.muke.demo.fragment;

import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.luocj.muke.demo.utils.Constant;
import com.luocj.muke.demo.utils.Utils;

public abstract class BaseFragment extends Fragment {
    private static final String TAG = BaseFragment.class.getSimpleName();
    //Fragment的View加载完毕的标记
    private boolean isViewCreated;

    //Fragment对用户可见的标记
    private boolean isUIVisible;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        isViewCreated = true;
        Log.i(TAG, "onViewCreated: " );
        lazyLoad();

    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        Log.i(TAG, "setUserVisibleHint: ");
        if (isVisibleToUser) {
            isUIVisible = true;
            lazyLoad();
        } else {
            isUIVisible = false;
        }
    }

    private void lazyLoad() {
        if (isViewCreated && isUIVisible) {
            loadData();
            //数据加载完毕,恢复标记,防止重复加载
            isViewCreated = false;
            isUIVisible = false;
        }

    }

    protected abstract void loadData();

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public boolean hasPermission(String... permissions) {
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(getActivity(), permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    public void requestPermission(int code, String... permissions) {
        if (Build.VERSION.SDK_INT >= 23) {
            requestPermissions(permissions, code);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case Constant.CAMERA_CODE:
                boolean cameraPermissionGranted = true;
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_DENIED) {
                    cameraPermissionGranted = false;
                    if (!ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), permissions[0])) {
                        Log.i(TAG, "onRequestPermissionsResult: " + "不在询问");
                        openCameraNeverAskAgain();
                    } else {
                        Log.i(TAG, "onRequestPermissionsResult: " + "权限失败");
                        openCameraFailed();
                    }
                } else {
                    Log.i(TAG, "onRequestPermissionsResult: " + "success");
                    openCamera();
                }
                break;

            case Constant.READ_SDCARD:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_DENIED) {
                    if (!ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), permissions[0])) {
                        Log.i(TAG, "onRequestPermissionsResult: " + "不在询问");
                        sdcardPermissionNeverAskAgain();
                    } else {
                        Log.i(TAG, "onRequestPermissionsResult: " + "权限失败");
                        sdcardPermissionFailed();
                    }
                } else {
                    Log.i(TAG, "onRequestPermissionsResult: " + "success");
                    sdcardPermission();
                }
                break;

            case Constant.CAMERA_SDCARD_CODE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_DENIED) {
                    cameraPermissionGranted = false;
                    if (!ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), permissions[0])) {
                        Log.i(TAG, "onRequestPermissionsResult: " + "不在询问");
                        openCameraNeverAskAgain();
                    } else {
                        Log.i(TAG, "onRequestPermissionsResult: " + "权限失败");
                        openCameraFailed();
                    }
                } else {
                    Log.i(TAG, "onRequestPermissionsResult: " + "success");
                    openCamera();
                }

                if (grantResults.length > 0 && grantResults[1] == PackageManager.PERMISSION_DENIED) {
                    if (!ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), permissions[1])) {
                        Log.i(TAG, "onRequestPermissionsResult: " + "不在询问");
                        sdcardPermissionNeverAskAgain();
                    } else {
                        Log.i(TAG, "onRequestPermissionsResult: " + "权限失败");
                        sdcardPermissionFailed();
                    }
                } else {
                    Log.i(TAG, "onRequestPermissionsResult: " + "success");
                    sdcardPermission();
                }
                break;
        }
    }

    public void sdcardPermission() {

    }

    public void sdcardPermissionFailed() {

    }

    public void sdcardPermissionNeverAskAgain() {

    }

    public void openCameraNeverAskAgain() {

    }

    public void openCameraFailed() {

    }

    public void openCamera() {

    }
}
